'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "";
            $scope.imgData={};
            $scope.defaultImgData={};
            if($state.includes('**.channel.update')){
                title="编辑平台基本信息";
                var id = $state.params.id;
                detail(id);
                $scope.isDisabled = false;
                $scope.flagShowOrHide=1;//用来控制排序号和是否可用是否显示
            }else if($state.includes('**.channel.create')){

                title="添加平台基本信息";
                $scope.record = {'bannerType' :1};
                $scope.isDisabled = false;
            }
            $scope.title = $rootScope.title = title;
            $scope.loading = true;

            //初始化验证
            $scope.submit= function(){
                $scope.loading = true;

                if(!$('#channelName').val()){
                    $scope.alertModal({'channelName':'渠道名称不能为空'});
                    return;
                }

                if(!$('#channelDesc').val()){
                    $scope.alertModal({'content':'描述不能为空'});
                    return;
                }

                if(!$('#source').val()){
                    $scope.alertModal({'content':'渠道来源标识不能为空'});
                    return;
                }

                if(!$('#sortNum').val()){
                    $scope.alertModal({'content':'排序号不能为空'});
                    return;
                }
                if(isNaN($('#sortNum').val())){
                    $scope.alertModal({'content':'排序号格式不正确'});
                    return;
                }
                var picUrl = $scope.imgData[0].src;
                if (!picUrl) {
                    $scope.alertModal({'content':'请上传带小红点的logo'});
                    return;
                }
                $scope.record.icon = picUrl[0];

                var defaultPicUrl = $scope.imgDatas[0].src;
                if (!defaultPicUrl) {
                    $scope.alertModal({'content':'请上传不带小红点的logo'});
                    return;
                }
                $scope.record.defaultIcon = defaultPicUrl[0];

                saveData();
            };

            function saveData(){
                var m = $scope.record;
                console.log(m);
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url : $scope.record.channelNo ? $scope.app.project_name+'/thirdPlatform/channel/update' : $scope.app.project_name+'/thirdPlatform/channel/add',
                        data: m
                    }).then(callback);
                }
                function callback(result){
                    console.log(result);
                    if(result.httpCode ==200){
                        $scope.alertModal({"content":"操作成功"});
                        $timeout(function(){
                            $state.go('main.hui.channel.list');
                        },2000);
                    }else{
                        $scope.alertModal({"content":"操作失败"});
                        $scope.isDisabled = false;
                    }
                    $scope.loading = false;
                }
            }

            // 初始化页面
            function detail(id) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/thirdPlatform/channel/detail',
                    data: {'id': id}
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                        $scope.imgData = [];
                        $scope.imgDatas = [];
                        var srcArr = [];

                        srcArr.push($scope.record.icon);
                        $scope.imgData.push({
                            src:srcArr
                        })
                        var srcArrs = [];
                        srcArrs.push($scope.record.defaultIcon);
                        $scope.imgDatas.push({
                            src:srcArrs
                        })
                        console.log($scope.imgDatas)
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }



        }]);